package com.hongxun.project.assets.pDevice.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.iDevice.service.IIDeviceService;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.project.assets.pTask.domain.PTask;
import com.hongxun.project.assets.pTask.service.IPTaskService;
import com.hongxun.project.monitor.server.domain.Sys;
//import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pDevice.domain.PDevice;
import com.hongxun.project.assets.pDevice.service.IPDeviceService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 配电设备采集 信息操作处理
 *
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pDevice")
public class PDeviceController extends BaseController {
    private String prefix = "assets/pDevice";

    @Autowired
    private IPDeviceService pDeviceService;
    @Autowired
    private IPBoxesService pBoxesService;
    @Autowired
    private IPPointService pPointService;
    @Autowired
    private IPTaskService pTaskService;
    @Autowired
    private IIDeviceService iDeviceService;




//    @RequiresPermissions("assets:pDevice:view")
    @GetMapping()
    public String pDevice(PDevice pDevice, ModelMap modelMap) {
        String pointId = pDevice.getPointId();

        PTask pTask = pTaskService.selectPTaskById(pDevice.getTaskId());

        PPoint pPoint = pPointService.selectPPointByIdForState(pointId, pDevice.getTaskId());
        modelMap.put("lineId", pPoint.getLineId());    // 线路ID
        modelMap.put("byqId", pPoint.getByqId());      // 变压器ID
        modelMap.put("taskId", pPoint.getTaskId());    // 任务ID
        modelMap.put("pointId", pointId);              // 点位ID
        modelMap.put("taskStatus", pTask.getTaskStatus());// 任务状态

        return prefix + "/pDevice";
    }

	/*@GetMapping("/task_pDevice/{taskId}")
    public String task_pDevice(@PathVariable("taskId") String taskId , ModelMap modelMap)
	{
		modelMap.put("taskId",taskId);
	    return prefix + "/task-pDevice";
	}*/

    /**
     * 查询配电设备采集列表
     */
//    @RequiresPermissions("assets:pDevice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PDevice pDevice) {
        startPage();
        List<PDevice> list = pDeviceService.selectPDeviceList(pDevice);
        return getDataTable(list);
    }


    /**
     * 导出配电设备采集列表
     */
//    @RequiresPermissions("assets:pDevice:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PDevice pDevice) {
        List<PDevice> list = pDeviceService.selectPDeviceList(pDevice);
        ExcelUtil<PDevice> util = new ExcelUtil<PDevice>(PDevice.class);
        return util.exportExcel(list, "pDevice");
    }

    /**
     * 新增配电设备采集
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存配电设备采集
     */
//    @RequiresPermissions("assets:pDevice:add")
    @Log(title = "配电设备采集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PDevice pDevice) {
        pDevice.setDatastate("新增");
        return toAjax(pDeviceService.insertPDevice(pDevice));
    }

    /**
     * 修改配电设备采集
     */
    @GetMapping("/edit/{id}/{taskId}")
    public String edit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap) {
        PDevice pDevice = pDeviceService.selectPDeviceByIdForState(id, taskId);
        mmap.put("pDevice", pDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存配电设备采集
     */
//    @RequiresPermissions("assets:pDevice:edit")
    @Log(title = "配电设备采集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PDevice pDevice) {
        pDevice.setDatastate("修改");
        return toAjax(pDeviceService.updatePDevice(pDevice));
    }

    /**
     * 删除配电设备采集
     */
//    @RequiresPermissions("assets:pDevice:remove")
    @Log(title = "配电设备采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pDeviceService.deletePDeviceByIds(ids));
    }

    /**
     * 删除配电设备采集
     */
//    @RequiresPermissions("assets:pDevice:remove")
    @Log(title = "配电设备采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove2Id")
    @ResponseBody
    public AjaxResult remove2Id(String ids , String taskIds) {

        String[] str = Convert.toStrArray(ids);
        String[] strTa = Convert.toStrArray(taskIds);
        int i = 0;
        for (String s : str) {
            PDevice pDevice = new PDevice();
            pDevice.setId(s);
            pDevice.setDatastate("删除");
            for (String s1 : strTa) {
                pDevice.setTaskId(s1);
                i = pDeviceService.updatePDevice(pDevice);
            }
            int ds = iDeviceService.deleteIDeviceByIds(s);
        }
        return toAjax(i);

//        return toAjax(pDeviceService.deletePDeviceByIdsAndTaskIds(ids , taskIds));
    }




}
